package com.eve.api.server;

import java.util.Collection;

import com.eve.api.client.APIRequestService;
import com.eve.api.server.account.characters.ApiCharacter;
import com.eve.api.server.account.characters.CharactersParser;
import com.eve.api.server.account.characters.CharactersResponse;
import com.eve.api.server.core.ApiAuthorization;
import com.eve.api.server.core.ApiException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

// The server side implementation of the RPC service.
@SuppressWarnings("serial")
public class APIRequestServiceImpl extends RemoteServiceServlet
	implements APIRequestService {
    private static final int EVE_USER_ID = 7861775;
    private static final String EVE_API_KEY = "C7DD662081344BD88A880AF95AD200652195386FF24B4A9AA5DEE11CD215F449";
    private CharactersResponse response = null;
    
	@Override
	public String getCharacters() {
		ApiAuthorization auth = new ApiAuthorization(EVE_USER_ID, 90828716, EVE_API_KEY);
		CharactersParser parser = new CharactersParser();
		try {
			response = parser.getResponse(auth);
		} catch (ApiException e) { }
		if(response != null){
			Collection<ApiCharacter> characters = response.getEveCharacters();
			synchronized (APIRequestServiceImpl.class) {
				for(ApiCharacter character : characters) {
					character.persist();
				}
			}
		}
		return null;
	}
}
